package com.v5.mr.selfjoin;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class SelfJoinReducer extends Reducer<LongWritable, Text, Text, Text> {

	@Override
	protected void reduce(LongWritable k3, Iterable<Text> v3, Context context)
			throws IOException, InterruptedException {
		// 定义变量：老板姓名  员工姓名
		String bossName = "";
		String empNameList = "";
		
		for(Text v:v3){
			String str = v.toString();
			
			//判断是否存在*号
			int index = str.indexOf("*");
			if(index >=0){
				//代表老板姓名
				bossName = str.substring(1);
			}else{
				//代表员工姓名
				empNameList = str + ";" + empNameList;
			}
		}
		
		//输出
		//判断：如果存在老板姓名和员工姓名，才输出
		if(bossName.length() > 0 && empNameList.length() > 0)
			context.write(new Text(bossName), new Text("("+empNameList+")"));
	}
}
















